import { Component,ViewChild} from '@angular/core';
import { NavController,Slides } from 'ionic-angular';
import { PlatformsPage } from './platforms/platforms'
import { HTTP } from '@ionic-native/http';
import { HttpService } from '../../service/httpService';
import { NativeService } from '../../service/NativeService';
import { GamesPage } from './games/games';
import { Toast } from '@ionic-native/toast';
import { Events } from 'ionic-angular';
import { LoginPage } from '../account/login/login';
import { UserService } from '../../service/userService';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  private banners:string[];
  private broadcast:any;
  private platforms:any;
  private showBroadcast:boolean = false;
  private games:any;
  private showHotGames:boolean = false;
  @ViewChild(Slides) slides: Slides;
  constructor(public navCtrl: NavController,
              public http:HTTP,
              private toast:Toast,
              private event:Events,
              public user:UserService,
              public httService:HttpService,
              public nat:NativeService) {
    this.initData();
    event.subscribe("loginAction",(user,time)=>{
      this.navCtrl.push(LoginPage)
    });
  }

  initData(){
    this.banners = [
      './assets/banner/banner1.png',
      './assets/banner/banner2.png',
      './assets/banner/banner3.png',
      './assets/banner/banner4.png',
      './assets/banner/banner5.png',
    ];
    this.platforms = [
      {
        icon:'./assets/dash/ag-live-dash.png',
        name:'AG真人',
        platform:'AG',
        isLink:true,
        url:"https://www.baidu.com"
      },
      {
        icon:'./assets/dash/bbin-live-dash.png',
        name:'BBIN真人',
        platform:'BBIN',
        isLink:true,
        url:"https://www.baidu.com"
      },
      {
        icon:'./assets/dash/gg-lottery-dash.png',
        name:'GG彩票',
        platform:'GG',
        isLink:true,
        url:"https://www.baidu.com"
      },
      {
        icon:'./assets/dash/hb-sport-dash.png',
        name:'沙巴体育',
        platform:'SB',
        isLink:true,
        url:"https://www.baidu.com"
      },
      {
        icon:'./assets/dash/pt-game-dash.png',
        name:'PT电子',
        platform:'PT',
        isLink:false,
        url:"https://www.baidu.com"
      },
      {
        icon:'./assets/dash/mg-game-dash.png',
        name:'GM电子',
        platform:'GM',
        isLink:false,
        url:"https://www.baidu.com"
      },
      {
        icon:'./assets/dash/ag-live-dash.png',
        name:'AG捕鱼王',
        platform:'AG',
        isLink:true,
        url:"https://www.baidu.com"
      },
      {
        icon:'./assets/dash/bbin-live-dash.png',
        name:'BBIN体育',
        platform:'AG',
        isLink:true,
        url:"https://www.baidu.com"
      },
    ];
  }

  ionViewDidLoad(){
    setTimeout(()=>{
      this.loadData();
    },1500)
  }

  loginAction(){
    this.navCtrl.push(LoginPage)
  }


  //加载数据
  loadData(){
    let self = this;
    let map = {
      "isMobile":true,
      "isHot":true,
      "platform":"",
      "pageSize":"12"
    };
    // //获取热门游戏
    self.httService.get("/api/v2/Game/GetHotGame",map,function (res,err) {
      if(!err){
        let obj = res.list;
        for(var i=0;i<obj.length;i++){
          obj[i].ImageUrl = self.httService.imageUrl+obj[i].ImageUrl;
        }
        self.games = obj;
        if (obj.length > 0){
          self.showHotGames = true;
        }
      }
    });
    this.httService.get("/api/News/GetBroadcast",null,function (res,err) {
      if (!err){
        self.broadcast = res;
        self.showBroadcast = true;
      }
    });
  }

  //玩游戏
  playGameAction1(index){
    let obj = this.platforms[index];
    if (obj.isLink){
      this.toast.show("playGameAction",JSON.stringify({url:obj.url}),'center').subscribe( toast => {
        console.log(toast)
      });
    }else {
      this.navCtrl.push(GamesPage,{platform:obj.platform});
    }
  }

  //玩游戏
  playGameAction(index){
    let data = this.games[index];
    let jsonString = JSON.stringify(data);
    this.toast.show("playGameAction",jsonString,'center').subscribe( toast => {
      console.log(toast)
    });
  }

  backdropclick(e){
    console.log(e);
  }

  //更多
  moreAction(index){
    if(index == 1){
      this.navCtrl.push(PlatformsPage);
    }else if(index == 2){
      this.navCtrl.push(GamesPage);
    }
  }


}
